//:ChallengeCupActivity.java
package com.xigua.xcar;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TabHost;

import com.xigua.xcar.common.CommonActivity;
import com.xigua.xcar.nearby.NearbyActivity;
import com.xigua.xcar.search.SearchActivity;
import com.xigua.xcar.setting.SettingActivity;

/**
 * ��activity
 * 
 * @author Vicent.Shen
 * @version 2012.3.12
 * */
public class MainTabActivity extends TabActivity {

	private static final String tag = "MainTabActivity";
	/********** private *********************/

	public static final int TAB_NEARBY = 0;
	public static final int TAB_SEARCH = 1;
	public static final int TAB_COMMON = 2;
	public static final int TAB_SETTING = 3;

	private static final String TAB_NEARBY_NAME = "nearby_tab";
	private static final String TAB_SEARCH_NAME = "search_tab";
	private static final String TAB_COMMON_NAME = "common_tab";
	private static final String TAB_SETTING_NAME = "setting_tab";
	private RadioGroup mRadioGroup;
	private int curTab = 0;
	public RadioGroup getRadioGroup() {
		return mRadioGroup;
	}

	private TabHost mTabHost;

	private OnCheckedChangeListener mTabRadioListener = new OnCheckedChangeListener() {

		public void onCheckedChanged(RadioGroup group, int checkedId) {
//			mRadioGroup.getChildAt(0).setBackgroundResource(
//					R.drawable.main_tab_selector);
//			((RadioButton) mRadioGroup.getChildAt(0))
//					.setCompoundDrawablesWithIntrinsicBounds(null,
//							getResources()
//									.getDrawable(R.drawable.btn_common_sele),
//							null, null);
			switch (checkedId) {
			case R.id.tab_nearby: {
				mTabHost.setCurrentTab(TAB_NEARBY);
//				((RadioButton) mRadioGroup.getChildAt(0))
//						.setCompoundDrawablesWithIntrinsicBounds(
//								null,
//								getResources().getDrawable(
//										R.drawable.btn_common_sele), null, null);
				View view = mRadioGroup.getChildAt(0);
				ImageView im = (ImageView)view.findViewById(R.id.image_maintab_one);
				im.setBackgroundResource(R.drawable.btn_nearby_sele);
				view = mRadioGroup.getChildAt(1);
				im = (ImageView)view.findViewById(R.id.image_maintab_two);
				im.setBackgroundResource(R.drawable.btn_search);
				view = mRadioGroup.getChildAt(2);
				im = (ImageView)view.findViewById(R.id.image_maintab_three);
				im.setBackgroundResource(R.drawable.btn_common);
				view = mRadioGroup.getChildAt(3);
				im = (ImageView)view.findViewById(R.id.image_maintab_four);
				im.setBackgroundResource(R.drawable.btn_setting);
//				((RadioButton) mRadioGroup.getChildAt(0)).setButtonDrawable(R.drawable.btn_common_sele);
//				((RadioButton) mRadioGroup.getChildAt(0))
//				.setBackgroundResource(R.drawable.btn_common_sele);
//				((RadioButton) mRadioGroup.getChildAt(1))
//						.setCompoundDrawablesWithIntrinsicBounds(
//								null,
//								getResources().getDrawable(
//										R.drawable.btn_nearby), null, null);
//				((RadioButton) mRadioGroup.getChildAt(2))
//						.setCompoundDrawablesWithIntrinsicBounds(null,
//								getResources()
//										.getDrawable(R.drawable.btn_common),
//								null, null);
//				((RadioButton) mRadioGroup.getChildAt(3))
//				.setCompoundDrawablesWithIntrinsicBounds(null,
//						getResources()
//								.getDrawable(R.drawable.btn_nearby),
//						null, null);
				break;
			}
			case R.id.tab_search: {
				mTabHost.setCurrentTab(TAB_SEARCH);
				View view = mRadioGroup.getChildAt(0);
				ImageView im = (ImageView)view.findViewById(R.id.image_maintab_one);
				im.setBackgroundResource(R.drawable.btn_nearby);
				view = mRadioGroup.getChildAt(1);
				im = (ImageView)view.findViewById(R.id.image_maintab_two);
				im.setBackgroundResource(R.drawable.btn_search_sele);
				view = mRadioGroup.getChildAt(2);
				im = (ImageView)view.findViewById(R.id.image_maintab_three);
				im.setBackgroundResource(R.drawable.btn_common);
				view = mRadioGroup.getChildAt(3);
				im = (ImageView)view.findViewById(R.id.image_maintab_four);
				im.setBackgroundResource(R.drawable.btn_setting);
//				((RadioButton) mRadioGroup.getChildAt(0))
//						.setCompoundDrawablesWithIntrinsicBounds(null,
//								getResources()
//										.getDrawable(R.drawable.btn_common),
//								null, null);
//				((RadioButton) mRadioGroup.getChildAt(0)).setButtonDrawable(R.drawable.btn_common);
////				((RadioButton) mRadioGroup.getChildAt(0))
////				.setBackgroundResource(R.drawable.btn_common);
//				((RadioButton) mRadioGroup.getChildAt(1))
//						.setCompoundDrawablesWithIntrinsicBounds(
//								null,
//								getResources().getDrawable(
//										R.drawable.btn_nearby_sele), null,
//								null);
//				((RadioButton) mRadioGroup.getChildAt(2))
//						.setCompoundDrawablesWithIntrinsicBounds(null,
//								getResources()
//										.getDrawable(R.drawable.btn_common),
//								null, null);
//				((RadioButton) mRadioGroup.getChildAt(3))
//				.setCompoundDrawablesWithIntrinsicBounds(null,
//						getResources()
//								.getDrawable(R.drawable.btn_nearby),
//						null, null);
				break;
			}
			case R.id.tab_common: {
				mTabHost.setCurrentTab(TAB_COMMON);
				View view = mRadioGroup.getChildAt(0);
				ImageView im = (ImageView)view.findViewById(R.id.image_maintab_one);
				im.setBackgroundResource(R.drawable.btn_nearby);
				view = mRadioGroup.getChildAt(1);
				im = (ImageView)view.findViewById(R.id.image_maintab_two);
				im.setBackgroundResource(R.drawable.btn_search);
				view = mRadioGroup.getChildAt(2);
				im = (ImageView)view.findViewById(R.id.image_maintab_three);
				im.setBackgroundResource(R.drawable.btn_common_sele);
				view = mRadioGroup.getChildAt(3);
				im = (ImageView)view.findViewById(R.id.image_maintab_four);
				im.setBackgroundResource(R.drawable.btn_setting);
//				((RadioButton) mRadioGroup.getChildAt(0))
//						.setCompoundDrawablesWithIntrinsicBounds(null,
//								getResources()
//										.getDrawable(R.drawable.btn_common),
//								null, null);
//				((RadioButton) mRadioGroup.getChildAt(0)).setButtonDrawable(R.drawable.btn_common);
////				.setBackgroundResource(R.drawable.btn_common);
//				((RadioButton) mRadioGroup.getChildAt(1))
//						.setCompoundDrawablesWithIntrinsicBounds(
//								null,
//								getResources().getDrawable(
//										R.drawable.btn_nearby), null, null);
//				((RadioButton) mRadioGroup.getChildAt(2))
//						.setCompoundDrawablesWithIntrinsicBounds(
//								null,
//								getResources().getDrawable(
//										R.drawable.btn_common_sele), null, null);
//				((RadioButton) mRadioGroup.getChildAt(3))
//				.setCompoundDrawablesWithIntrinsicBounds(null,
//						getResources()
//								.getDrawable(R.drawable.btn_nearby),
//						null, null);
				break;
			}
			case R.id.tab_setting: {
				mTabHost.setCurrentTab(TAB_SETTING);
				View view = mRadioGroup.getChildAt(0);
				ImageView im = (ImageView)view.findViewById(R.id.image_maintab_one);
				im.setBackgroundResource(R.drawable.btn_nearby);
				view = mRadioGroup.getChildAt(1);
				im = (ImageView)view.findViewById(R.id.image_maintab_two);
				im.setBackgroundResource(R.drawable.btn_search);
				view = mRadioGroup.getChildAt(2);
				im = (ImageView)view.findViewById(R.id.image_maintab_three);
				im.setBackgroundResource(R.drawable.btn_common);
				view = mRadioGroup.getChildAt(3);
				im = (ImageView)view.findViewById(R.id.image_maintab_four);
				im.setBackgroundResource(R.drawable.btn_setting_sele);
//				((RadioButton) mRadioGroup.getChildAt(0))
//						.setCompoundDrawablesWithIntrinsicBounds(null,
//								getResources()
//										.getDrawable(R.drawable.btn_common),
//								null, null);
//				((RadioButton) mRadioGroup.getChildAt(0)).setButtonDrawable(R.drawable.btn_common);
////				.setBackgroundResource(R.drawable.btn_common);
//				((RadioButton) mRadioGroup.getChildAt(1))
//						.setCompoundDrawablesWithIntrinsicBounds(
//								null,
//								getResources().getDrawable(
//										R.drawable.btn_nearby), null, null);
//				((RadioButton) mRadioGroup.getChildAt(2))
//						.setCompoundDrawablesWithIntrinsicBounds(
//								null,
//								getResources().getDrawable(
//										R.drawable.btn_common), null, null);
//				((RadioButton) mRadioGroup.getChildAt(3))
//				.setCompoundDrawablesWithIntrinsicBounds(null,
//						getResources()
//								.getDrawable(R.drawable.btn_nearby_sele),
//						null, null);
				break;
			}
			default:
				break;
			}
		}
	};

	/**************************************/
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i(tag, "onCreate");
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.cmaintab);
		//
		mRadioGroup = (RadioGroup) findViewById(R.id.main_radio);

		//
		mTabHost = getTabHost();

		//
		mRadioGroup.setOnCheckedChangeListener(mTabRadioListener);
		RelativeLayout layout = (RelativeLayout)mRadioGroup.getChildAt(0);
		layout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				curTab = TAB_NEARBY;
				mTabRadioListener.onCheckedChanged(mRadioGroup, R.id.tab_nearby);
			}
		});
		layout = (RelativeLayout)mRadioGroup.getChildAt(1);
		layout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				curTab = TAB_SEARCH;
				mTabRadioListener.onCheckedChanged(mRadioGroup, R.id.tab_search);
			}
		});
		layout = (RelativeLayout)mRadioGroup.getChildAt(2);
		layout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				curTab = TAB_COMMON;
				mTabRadioListener.onCheckedChanged(mRadioGroup, R.id.tab_common);
			}
		});
		layout = (RelativeLayout)mRadioGroup.getChildAt(3);
		layout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				curTab = TAB_SETTING;
				mTabRadioListener.onCheckedChanged(mRadioGroup, R.id.tab_setting);
			}
		});
		//
		initTabs();
	}

	/**
	 * 
	 * */
	private void initTabs() {
		Intent intent;

		intent = new Intent(this, NearbyActivity.class);
		mTabHost.addTab(buildTabSpec(TAB_NEARBY_NAME,
				R.string.bottom_text_nearby, R.drawable.btn_common, intent));

		intent = new Intent(this, SearchActivity.class);
		mTabHost.addTab(buildTabSpec(TAB_SEARCH_NAME,
				R.string.bottom_text_search, R.drawable.btn_nearby, intent));

		intent = new Intent(this, CommonActivity.class);
		mTabHost.addTab(buildTabSpec(TAB_COMMON_NAME,
				R.string.bottom_text_common, R.drawable.btn_common, intent));

		intent = new Intent(this, SettingActivity.class);
		mTabHost.addTab(buildTabSpec(TAB_SETTING_NAME,
				R.string.bottom_text_setting, R.drawable.btn_nearby, intent));

		// intent.putExtra(CommonConst.ACTION_ID, actionId);
		// switch (actionId) {
		// case TAB_HOMEPAGE_ID:
		// mRadioGroup.check(R.id.tab_homepage);
		// break;
		// case TAB_GAMESCENTER_ID:
		// mRadioGroup.check(R.id.tab_gamescenter);
		// break;
		// case TAB_PERSONALHOME_ID:
		// mRadioGroup.check(R.id.tab_personalhome);
		// break;
		// }
		mRadioGroup.check(R.id.tab_nearby);
	}

	/**
	 * 
	 * @param tag
	 *            :
	 * @param resLabel
	 * @param resIcon
	 *            :
	 * @param content
	 *            :
	 * */
	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
			final Intent content) {
		return this.mTabHost
				.newTabSpec(tag)
				.setIndicator(getString(resLabel),
						getResources().getDrawable(resIcon))
				.setContent(content);
	}
	
	private static final String CURTAB = "CURTAB";
	
	private void saveCurTabToSP(int tab){
		SharedPreferences sp = this.getPreferences(MODE_WORLD_WRITEABLE);
		Editor edit = sp.edit();
		edit.putInt(CURTAB, tab);
		edit.commit();
	}

	private int getCurTabFromSP(){
		int tab = 0;
		SharedPreferences sp = this.getPreferences(MODE_WORLD_READABLE);
		tab = sp.getInt(CURTAB, 0);
		return tab;
	}
	
	private void clearCurTab(){
		SharedPreferences sp = this.getPreferences(MODE_WORLD_WRITEABLE);
		Editor edit = sp.edit();
		edit.clear();
		edit.commit();
	}
	
	protected void onDestroy() {
		clearCurTab();
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		Log.i(tag, "onResume");
		int ret = getCurTabFromSP();
		switch(ret){
		case TAB_NEARBY:{
			mRadioGroup.check(R.id.tab_nearby);
			break;
		}
		case TAB_SEARCH:{
			mRadioGroup.check(R.id.tab_search);
			break;
		}
		case TAB_COMMON:{
			mRadioGroup.check(R.id.tab_common);
			break;
		}
		case TAB_SETTING:{
			mRadioGroup.check(R.id.tab_setting);
			break;
		}
		}
		super.onResume();
	}

	@Override
	protected void onPause() {
		Log.i(tag, "onPause");
		saveCurTabToSP(curTab);
		super.onResume();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.d(tag, "back");
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { //按下的如果是BACK，同时没有重复
	    	 AlertDialog.Builder builder = new AlertDialog.Builder(this);  
	    	    builder.setTitle("提示");  
	    	    builder.setMessage("确定要退出程序吗?");  
	    	    builder.setPositiveButton("确定",  
	    	            new DialogInterface.OnClickListener() {  
	    	                public void onClick(DialogInterface dialog, int whichButton) {           
	    	                	MainTabActivity.this.finish();  
	    	                }  
	    	            });  
	    	    builder.setNegativeButton("取消",  
	    	            new DialogInterface.OnClickListener() {  
	    	                public void onClick(DialogInterface dialog, int whichButton) {  
	    	                }  
	    	            }).create(); 
	    	
	    	
	    	    
	        return true;
	    }
		return super.onKeyDown(keyCode, event);
	}

	BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
		}
	};
	
	
}
// /:~